package org.xiyinjihe.platform.filter;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * Created by sunwen on 2018/11/14.
 * (๑•̀ㅂ•́)و✧
 */
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {

    private HttpServletRequest orgRequest = null;

    private final static String[] ignUrl = {
            "script", "mid", "master", "truncate", "declare",
            "iframe", "'", "onreadystatechange", "alert", "atestu", "xss", ";", "'", "\"", "<", ">", "(", ")", ",",
            "\\", "svg", "confirm", "prompt", "onload", "onmouseover", "onfocus", "onerror"
    };

    public XssHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
        orgRequest = request;
    }

    /**
     * GRT请求参数拦截
     * @param parameter
     * @return
     */
    @Override
    public String[] getParameterValues(String parameter) {
        String[] values = super.getParameterValues(parameter);
        if (values==null)  {
            return null;
        }
        int count = values.length;
        String[] encodedValues = new String[count];
        for (int i = 0; i < count; i++) {
            encodedValues[i] = values[i];
            xssFilter(values[i]);
        }
        return encodedValues;
    }

    @Override
    public String getParameter(String parameter) {
       String value = super.getParameter(parameter);
        if (value != null) {
            return value;
        }
        return null;
    }

    @Override
    public String getHeader(String name) {
        String value = super.getHeader(name);
        if (value == null)
            return null;
        return value;
    }

    /**
     * POST请求参数拦截
     * @return
     * @throws IOException
     */
    @Override
    public ServletInputStream getInputStream() throws IOException {
        String params = inputHandlers(super.getInputStream());
        final ByteArrayInputStream bais = new ByteArrayInputStream(params.getBytes());
//        String params = inputHandlers(super.getInputStream());
        return new ServletInputStream() {

            @Override
            public int read() throws IOException {
                return bais.read();
            }

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) { }
        };
    }

    public   String inputHandlers(ServletInputStream servletInputStream){
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(servletInputStream, Charset.forName("UTF-8")));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (servletInputStream != null) {
                try {
                    servletInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  sb.toString();
    }

    public String xssFilter(String item){
        if(item.length()>200){
            return item;
        }
//        String regex = "^[A-Za-z0-9\\u4e00-\\u9fa5]+$";
//        boolean flag = item.matches(regex);
//        if(!flag){
//            System.out.println(item);
//            throw new CommonException(CommonError.PARAMS_ERROR);
//        }
//        for(String ign : ignUrl){
//            if(item.contains(ign)){
//                System.out.println(item);
//                System.out.println(ign);
//                throw new CommonException(CommonError.PARAMS_ERROR);
//            }
//        }
        return item;
    }

}
